set more off
clear all

*1984 - special year (updated files are uploaded)
*1994, 1993 - fifth quarter is missing, use next year first quarter instead (copy-paste in files)

*for 1980-2010 code closely follows Aguir-Bils
*for 2011- it is extended to get similar variables

local start_year=1980
local finish_year=2018




forvalues year=`start_year'(1)`finish_year' {
if `year'==1980 | `year'==1981 { 
	local number=8423
}
if `year'==1983 | `year'==1982 { 
	local number=8598
}
if `year'==1984 { 
	local number=8671
}
if `year'==1985 { 
	local number=8904
}
if `year'==1986 { 
	local number=9113
}
if `year'==1987 { 
	local number=9332
}
if `year'==1988 { 
	local number=9451
}
if `year'==1989 { 
	local number=9712
}
if `year'==1990 { 
	local number=9820
}
if `year'==1991 { 
	local number=6209
}
if `year'==1992 { 
	local number=6372
}
if `year'==1993 { 
	local number=6580
}
if `year'==1994 { 
	local number=6710
}
if `year'==1995 { 
	local number=2264
}


forvalues quarter=1(1)5{





if `year'>=1995 | (`year'==1994 & `quarter'==5) {



	if `year'==1995 | (`year'==1994 & `quarter'==5) {

		if `quarter'==1 {
			infile using dictionary_fmly_1995, using("cex_files_old/`year'/DS0001/0`number'-0001-Data.txt") clear
		}
		if `quarter'==2 {
			infile using dictionary_fmly_1995, using("cex_files_old/`year'/DS0005/0`number'-0005-Data.txt") clear
		}
		if `quarter'==3 {
			infile using dictionary_fmly_1995, using("cex_files_old/`year'/DS0009/0`number'-0009-Data.txt") clear
		}
		if `quarter'==4 {
			infile using dictionary_fmly_1995, using("cex_files_old/`year'/DS0013/0`number'-0013-Data.txt") clear
		}
		if `quarter'==5 {
			infile using dictionary_fmly_1995, using("cex_files_old/`year'/DS0017/0`number'-0017-Data.txt") clear
		}
		
		
		gen educ_ref_1d=educ_ref
		gen educ_2_1d=educa2
		gen qintrvyr="19"+qintrvy2
		
	}
	

	if `year'>1995 {
	
		local YY=mod(`year',100)
		local YY_next=mod(`year'+1,100)

		if `year'<2000 | `year'>=2010 {
			local add=""
			local add_next=""
		}
		if `year'>=2000 & `year'<2010 {
			local add="0"
			local add_next="0"
		}
		if `year'==1999 & `quarter'==5 {
			local add_next="0"
		}
		if `year'==2009 & `quarter'==5 {
			local add_next=""
		}




		if `quarter'==1 {
			use "cex_files/intrvw`add'`YY'/fmli`add'`YY'1x.dta", clear
		}
		if `quarter'==5 {
			use "cex_files/intrvw`add'`YY'/fmli`add_next'`YY_next'1.dta", clear 
		}
		if `quarter'>1 & `quarter'<5 {
			use "cex_files/intrvw`add'`YY'/fmli`add'`YY'`quarter'.dta", clear 
		}
	}	


	*keep necessary variables

	if (`year'==2001 & `quarter'==1) | (`year'>=1996 & `year'<=2000) {

		keep newid qintrvmo qintrvyr finlwt21 bls_urbn age_ref sex_ref age2 sex2 marital1 fam_size no_earnr educ_ref educa2 fsalaryx fsal_ryx fincbtax fincbt_x fincatax fincat_x /*
		*/ fjssdedx frrdedx fgovretx fpripenx findretx nonincmx pensionx fsalaryx fincbtax respstat totexppq totexpcq fsltaxx sloctaxx taxpropx misctaxx slrfundx othrfndx proptxpq /*
		*/ proptxcq frretirx welfarex unemplx perslt18 famtfedx fedtaxx fedrfndx fdhomepq fdhomecq fdawaypq fdawaycq alcbevpq alcbevcq rendwepq rendwecq othlodpq othlodcq utilpq utilcq /*
		*/ dmsxccpq dmsxcccq bbydaypq bbydaycq textilpq textilcq furntrpq furntrcq flrcvrpq flrcvrcq majapppq majappcq smlapppq smlappcq misceqpq misceqcq telephpq telephcq mensixpq /*
		*/ mensixcq womsixpq womsixcq boyfifpq boyfifcq grlfifpq grlfifcq chldrnpq chldrncq footwrpq footwrcq othaplpq othaplcq cartknpq cartkncq cartkupq cartkucq othvehpq othvehcq /*
		*/ vehfinpq vehfincq vrntlopq vrntlocq vehinspq vehinscq gasmopq gasmocq mainrppq mainrpcq pubtrapq pubtracq healthpq healthcq feeadmpq feeadmcq readpq readcq tvrdiopq tvrdiocq /*
		*/ otheqppq otheqpcq perscapq perscacq educapq educacq tobaccpq tobacccq cashcopq cashcocq owndwepq owndwecq miscx4pq miscx4cq lifinspq lifinscq retpenpq retpencq fdmappq fdmapcq /*
		*/ inc_hrs2 inc_hrs1 incweek1 incweek2 region occucod1 occucod2


	}



	if (`year'==2001 & `quarter'>=2) | (`year'>=2002 & `year'<=2003) | (`year'>=2006 & `year'<=2010) {

		keep newid qintrvmo qintrvyr finlwt21 bls_urbn age_ref sex_ref age2 sex2 marital1 fam_size no_earnr educ_ref educa2 fsalaryx fsal_ryx fincbtax fincbt_x fincatax fincat_x /*
		*/ fjssdedx frrdedx fgovretx fpripenx findretx nonincmx pensionx fsalaryx fincbtax respstat totexppq totexpcq fsltaxx sloctaxx taxpropx misctaxx slrfundx othrfndx proptxpq /*
		*/ proptxcq frretirx welfarex unemplx perslt18 famtfedx fedtaxx fedrfndx fdhomepq fdhomecq fdawaypq fdawaycq alcbevpq alcbevcq rendwepq rendwecq othlodpq othlodcq utilpq utilcq /*
		*/ dmsxccpq dmsxcccq bbydaypq bbydaycq textilpq textilcq furntrpq furntrcq flrcvrpq flrcvrcq majapppq majappcq smlapppq smlappcq misceqpq misceqcq telephpq telephcq mensixpq /*
		*/ mensixcq womsixpq womsixcq boyfifpq boyfifcq grlfifpq grlfifcq chldrnpq chldrncq footwrpq footwrcq othaplpq othaplcq cartknpq cartkncq cartkupq cartkucq othvehpq othvehcq /*
		*/ vehfinpq vehfincq vrntlopq vrntlocq vehinspq vehinscq gasmopq gasmocq mainrppq mainrpcq pubtrapq pubtracq healthpq healthcq feeadmpq feeadmcq readpq readcq tvrdiopq tvrdiocq /*
		*/ otheqppq otheqpcq perscapq perscacq educapq educacq tobaccpq tobacccq cashcopq cashcocq owndwepq owndwecq miscx4pq miscx4cq lifinspq lifinscq retpenpq retpencq fdmappq fdmapcq /*
		*/ pnsionbx /*
		*/ inc_hrs2 inc_hrs1 incweek1 incweek2 region occucod1 occucod2
	
		replace pensionx=pensionx+pnsionbx


	}

	if `year'>=2011 & `year'<=2013 {
		keep newid qintrvmo qintrvyr finlwt21 bls_urbn age_ref sex_ref age2 sex2 marital1 fam_size no_earnr educ_ref educa2 fsalaryx fsal_ryx fincbtax fincbt_x fincatax /*
		*/ fjssdedx frrdedx fgovretx nonincmx totexppq totexpcq proptxpq respstat/*
		*/ proptxcq perslt18 fdhomepq fdhomecq fdawaypq fdawaycq alcbevpq alcbevcq rendwepq rendwecq othlodpq othlodcq utilpq utilcq /*
		*/ dmsxccpq dmsxcccq bbydaypq bbydaycq textilpq textilcq furntrpq furntrcq flrcvrpq flrcvrcq majapppq majappcq smlapppq smlappcq misceqpq misceqcq telephpq telephcq mensixpq /*
		*/ mensixcq womsixpq womsixcq boyfifpq boyfifcq grlfifpq grlfifcq chldrnpq chldrncq footwrpq footwrcq othaplpq othaplcq cartknpq cartkncq cartkupq cartkucq othvehpq othvehcq /*
		*/ vehfinpq vehfincq vrntlopq vrntlocq vehinspq vehinscq gasmopq gasmocq mainrppq mainrpcq pubtrapq pubtracq healthpq healthcq feeadmpq feeadmcq readpq readcq tvrdiopq tvrdiocq /*
		*/ otheqppq otheqpcq perscapq perscacq educapq educacq tobaccpq tobacccq cashcopq cashcocq owndwepq owndwecq miscx4pq miscx4cq lifinspq lifinscq retpenpq retpencq fdmappq fdmapcq /*
		*/ inc_hrs2 inc_hrs1 incweek1 incweek2 region occucod1 occucod2
	}
	if `year'>=2013 & `year'<=2014 {
		keep newid qintrvmo qintrvyr finlwt21 bls_urbn age_ref sex_ref age2 sex2 marital1 fam_size no_earnr educ_ref educa2 fsalaryx fsal_ryx fincbtax fincbt_x fincatax /*
		*/ fjssdedx frrdedx fgovretx nonincmx totexppq totexpcq proptxpq /*
		*/ proptxcq perslt18 fdhomepq fdhomecq fdawaypq fdawaycq alcbevpq alcbevcq rendwepq rendwecq othlodpq othlodcq utilpq utilcq /*
		*/ dmsxccpq dmsxcccq bbydaypq bbydaycq textilpq textilcq furntrpq furntrcq flrcvrpq flrcvrcq majapppq majappcq smlapppq smlappcq misceqpq misceqcq telephpq telephcq mensixpq /*
		*/ mensixcq womsixpq womsixcq boyfifpq boyfifcq grlfifpq grlfifcq chldrnpq chldrncq footwrpq footwrcq othaplpq othaplcq cartknpq cartkncq cartkupq cartkucq othvehpq othvehcq /*
		*/ vehfinpq vehfincq vrntlopq vrntlocq vehinspq vehinscq gasmopq gasmocq mainrppq mainrpcq pubtrapq pubtracq healthpq healthcq feeadmpq feeadmcq readpq readcq tvrdiopq tvrdiocq /*
		*/ otheqppq otheqpcq perscapq perscacq educapq educacq tobaccpq tobacccq cashcopq cashcocq owndwepq owndwecq miscx4pq miscx4cq lifinspq lifinscq retpenpq retpencq fdmappq fdmapcq /*
		*/ inc_hrs2 inc_hrs1 incweek1 incweek2 region occucod1 occucod2
		
		gen respstat="1"
	}
	if `year'==2015 & `quarter'<=4 {
		keep newid qintrvmo qintrvyr finlwt21 bls_urbn age_ref sex_ref age2 sex2 marital1 fam_size no_earnr educ_ref educa2 fsalaryx fsal_ryx fincbtax fincbt_x finatxem /*
		*/ fjssdedx frrdedx fgovretx nonincmx totexppq totexpcq proptxpq desflg /*
		*/ proptxcq perslt18 fdhomepq fdhomecq fdawaypq fdawaycq alcbevpq alcbevcq rendwepq rendwecq othlodpq othlodcq utilpq utilcq /*
		*/ dmsxccpq dmsxcccq bbydaypq bbydaycq textilpq textilcq furntrpq furntrcq flrcvrpq flrcvrcq majapppq majappcq smlapppq smlappcq misceqpq misceqcq telephpq telephcq mensixpq /*
		*/ mensixcq womsixpq womsixcq boyfifpq boyfifcq grlfifpq grlfifcq chldrnpq chldrncq footwrpq footwrcq othaplpq othaplcq cartknpq cartkncq cartkupq cartkucq othvehpq othvehcq /*
		*/ vehfinpq vehfincq vrntlopq vrntlocq vehinspq vehinscq gasmopq gasmocq mainrppq mainrpcq pubtrapq pubtracq healthpq healthcq feeadmpq feeadmcq readpq readcq tvrdiopq tvrdiocq /*
		*/ otheqppq otheqpcq perscapq perscacq educapq educacq tobaccpq tobacccq cashcopq cashcocq owndwepq owndwecq miscx4pq miscx4cq lifinspq lifinscq retpenpq retpencq fdmappq fdmapcq /*
		*/ inc_hrs2 inc_hrs1 incweek1 incweek2 region occucod1 occucod2
	
		rename finatxem fincatax
		gen respstat="1"
		destring newid, replace
	}

	if (`year'==2015 & `quarter'==5) | `year'>=2016  {
		keep newid qintrvmo qintrvyr finlwt21 bls_urbn age_ref sex_ref age2 sex2 marital1 fam_size no_earnr educ_ref educa2 fsalaryx fsal_ryx fincbtax fincbt_x finatxem /*
		*/ fjssdedx frrdedx fgovretx nonincmx totexppq totexpcq proptxpq/*
		*/ proptxcq perslt18 fdhomepq fdhomecq fdawaypq fdawaycq alcbevpq alcbevcq rendwepq rendwecq othlodpq othlodcq utilpq utilcq /*
		*/ dmsxccpq dmsxcccq bbydaypq bbydaycq textilpq textilcq furntrpq furntrcq flrcvrpq flrcvrcq majapppq majappcq smlapppq smlappcq misceqpq misceqcq telephpq telephcq mensixpq /*
		*/ mensixcq womsixpq womsixcq boyfifpq boyfifcq grlfifpq grlfifcq chldrnpq chldrncq footwrpq footwrcq othaplpq othaplcq cartknpq cartkncq cartkupq cartkucq othvehpq othvehcq /*
		*/ vehfinpq vehfincq vrntlopq vrntlocq vehinspq vehinscq gasmopq gasmocq mainrppq mainrpcq pubtrapq pubtracq healthpq healthcq feeadmpq feeadmcq readpq readcq tvrdiopq tvrdiocq /*
		*/ otheqppq otheqpcq perscapq perscacq educapq educacq tobaccpq tobacccq cashcopq cashcocq owndwepq owndwecq miscx4pq miscx4cq lifinspq lifinscq retpenpq retpencq fdmappq fdmapcq /*
		*/ inc_hrs2 inc_hrs1 incweek1 incweek2 region occucod1 occucod2
		
	
		rename finatxem fincatax
		gen respstat="1"
		destring newid, replace

	}

	if `year'==2004 | `year'==2005 {

		keep newid qintrvmo qintrvyr finlwt21 bls_urbn age_ref sex_ref age2 sex2 marital1 fam_size no_earnr educ_ref educa2 fsalarym fsal_rym fincbtxm fincb_xm fincatxm finca_xm /*
		*/ fjssdedm frrdedm fgovretm fpripenm findretx nonincmx pensionm fsalaryi fincbtxi respstat totexppq totexpcq fsltaxxm sloctaxx taxpropx misctaxx slrfundx othrfndx proptxpq /*
		*/ proptxcq frretirm welfarem unemplxm perslt18 famtfedm fedtaxx fedrfndx fdhomepq fdhomecq fdawaypq fdawaycq alcbevpq alcbevcq rendwepq rendwecq othlodpq othlodcq utilpq utilcq /*
		*/ dmsxccpq dmsxcccq bbydaypq bbydaycq textilpq textilcq furntrpq furntrcq flrcvrpq flrcvrcq majapppq majappcq smlapppq smlappcq misceqpq misceqcq telephpq telephcq mensixpq /*
		*/ mensixcq womsixpq womsixcq boyfifpq boyfifcq grlfifpq grlfifcq chldrnpq chldrncq footwrpq footwrcq othaplpq othaplcq cartknpq cartkncq cartkupq cartkucq othvehpq othvehcq /*
		*/ vehfinpq vehfincq vrntlopq vrntlocq vehinspq vehinscq gasmopq gasmocq mainrppq mainrpcq pubtrapq pubtracq healthpq healthcq feeadmpq feeadmcq readpq readcq tvrdiopq tvrdiocq /*
		*/ otheqppq otheqpcq perscapq perscacq educapq educacq tobaccpq tobacccq cashcopq cashcocq owndwepq owndwecq miscx4pq miscx4cq lifinspq lifinscq retpenpq retpencq fdmappq fdmapcq /*
		*/ inc_hrs2 inc_hrs1 incweek1 incweek2 region occucod1 occucod2

		rename fsalarym fsalaryx
		rename fincbtxm fincbtax
		rename fincatxm fincatax
		rename famtfedm famtfedx
		rename fsltaxxm fsltaxx
		rename fsal_rym fsal_ryx
		rename fincb_xm fincbt_x
		rename finca_xm fincat_x
		rename fjssdedm fjssdedx
		rename frrdedm frrdedx
		rename fgovretm fgovretx
		rename fpripenm fpripenx
		rename pensionm pensionx
		rename welfarem welfarex
		rename frretirm frretirx

		replace fincatax=0 if fincbtxi!=100 & (fincbtxi<301 | fincbtxi>399)
		replace fincbtax=0 if fincbtxi!=100 & (fincbtxi<301 | fincbtxi>399)
		replace famtfedx=0 if fincbtxi!=100 & (fincbtxi<301 | fincbtxi>399)
		replace fsltaxx=0 if fincbtxi!=100 & (fincbtxi<301 | fincbtxi>399)
		replace fsalaryx=0 if fsalaryi!=100 & (fsalaryi<301 | fsalaryi>399)


	}


	if `year'!=1995 & `year'!=1994 {
		rename educ_ref educ_ref_2d
		rename educa2 educ_2_2d
	}

	egen totexp2=rowtotal(totexp*)
	egen fdhome=rowtotal(fdhome*)
	egen fdaway=rowtotal(fdaway*)
	egen alcbev=rowtotal(alcbev*)
	egen shelt=rowtotal(rendwe* othlod*)
	egen teleph=rowtotal(teleph*)
	egen util=rowtotal(util*)
	replace util=util-teleph
	egen dmsxcc=rowtotal(dmsxcc*)
	egen bbyday=rowtotal(bbyday*)
	egen furnsh=rowtotal(textil* furntr* flrcvr*)
	egen equpmt=rowtotal(majapp* smlapp* misceq*)
	replace equpmt=equpmt+teleph
	egen mensix=rowtotal(mensix*)
	egen womsix=rowtotal(womsix*)
	egen chldrn=rowtotal(boyfif* grlfif* chldrn*)
	egen footwr=rowtotal(footwr*)
	egen othapl=rowtotal(othapl*)
	egen vehpch=rowtotal(cartkn* cartku* othveh* vehfin* vrntlo* vehins*)
	egen vehopr=rowtotal(gasmo* mainrp*)
	egen pubtra=rowtotal(pubtra*)
	egen health=rowtotal(health*)
	egen feeard=rowtotal(feeadm* read*)
	egen tvrdot=rowtotal(tvrdio* otheqp*)
	egen persca=rowtotal(persca*)
	egen educa=rowtotal(educapq* educacq*)
	egen tobacc=rowtotal(tobacc*)
	egen cashco=rowtotal(cashco*)
	egen owndwe=rowtotal(owndwe*)
	egen miscx4=rowtotal(miscx4*)
	egen lifins=rowtotal(lifins*)
	egen retpen=rowtotal(retpen*)
	egen fdmap=rowtotal(fdmap*)
	egen proptx=rowtotal(proptx*)

	capture gen desflg="00"
	
	keep newid qintrvmo qintrvyr bls_urbn age_ref finlwt21 sex_ref marital1 fam_size no_earnr educ_* age2 sex2 fsalaryx fsal_ryx fincbtax fincbt_x fincatax fjssdedx frrdedx fgovretx nonincmx respstat /*
	*/ fdhome fdaway alcbev shelt util dmsxcc bbyday furnsh equpmt mensix womsix chldrn footwr othapl vehpch vehopr pubtra health feeard tvrdot persca educa tobacc cashco owndwe miscx4 lifins retpen fdmap totexp2 perslt18 desflg /*
	*/ inc_hrs2 inc_hrs1 incweek1 incweek2 region occucod1 occucod2
 

}






if `year'<=1993 | (`year'==1994 & `quarter'!=5){

	if `year'>=1986 {
		if `quarter'==1 {
			infile using dictionary_fmly_1986_1994, using("cex_files_old/`year'/DS0001/0`number'-0001-Data.txt") clear
		}
		if `quarter'==2 {
			infile using dictionary_fmly_1986_1994, using("cex_files_old/`year'/DS0005/0`number'-0005-Data.txt") clear
		}
		if `quarter'==3 {
			infile using dictionary_fmly_1986_1994, using("cex_files_old/`year'/DS0009/0`number'-0009-Data.txt") clear
		}
		if `quarter'==4 {
			infile using dictionary_fmly_1986_1994, using("cex_files_old/`year'/DS0013/0`number'-0013-Data.txt") clear
		}
		if `quarter'==5 {
			infile using dictionary_fmly_1986_1994, using("cex_files_old/`year'/DS0017/0`number'-0017-Data.txt") clear
		}
	}
		
	if `year'==1985 {
		if `quarter'==1 {
			infile using dictionary_fmly_1985, using("cex_files_old/`year'/DS0001/0`number'-0001-Data.txt") clear
		}
		if `quarter'==2 {
			infile using dictionary_fmly_1985, using("cex_files_old/`year'/DS0005/0`number'-0005-Data.txt") clear
		}
		if `quarter'==3 {
			infile using dictionary_fmly_1985, using("cex_files_old/`year'/DS0009/0`number'-0009-Data.txt") clear
		}
		if `quarter'==4 {
			infile using dictionary_fmly_1985, using("cex_files_old/`year'/DS0013/0`number'-0013-Data.txt") clear
		}
		if `quarter'==5 {
			infile using dictionary_fmly_1985, using("cex_files_old/`year'/DS0017/0`number'-0017-Data.txt") clear
		}	
	}
	
	if `year'==1984 {
		if `quarter'==1 {
			use "cex_files_old/`year'/DS0001/0`number'-0001-Data.dta", clear
		}
		if `quarter'==2 {
			use "cex_files_old/`year'/DS0005/0`number'-0005-Data.dta", clear
		}
		if `quarter'==3 {
			use "cex_files_old/`year'/DS0009/0`number'-0009-Data.dta", clear
		}
		if `quarter'==4 {
			use "cex_files_old/`year'/DS0013/0`number'-0013-Data.dta", clear
		}
		if `quarter'==5 {
			use "cex_files_old/`year'/DS0017/0`number'-0017-Data.dta", clear
		}	
		
		rename *, lower /*make all lowercase*/
		rename qintrvyr qintrvy2
		rename jotaxnet misctaxx
	}
	
	if `year'==1982 {
		if `quarter'==1 {
			infile using dictionary_fmly_1985, using("cex_files_old/82_83/DS0001/0`number'-0001-Data.txt") clear
		}
		if `quarter'==2 {
			infile using dictionary_fmly_1985, using("cex_files_old/82_83/DS0005/0`number'-0005-Data.txt") clear
		}
		if `quarter'==3 {
			infile using dictionary_fmly_1985, using("cex_files_old/82_83/DS0009/0`number'-0009-Data.txt") clear
		}
		if `quarter'==4 {
			infile using dictionary_fmly_1985, using("cex_files_old/82_83/DS0013/0`number'-0013-Data.txt") clear
		}
		if `quarter'==5 {
			infile using dictionary_fmly_1985, using("cex_files_old/82_83/DS0017/0`number'-0017-Data.txt") clear
		}	
	}	
	if `year'==1983 {
		if `quarter'==1 {
			infile using dictionary_fmly_1985, using("cex_files_old/82_83/DS0017/0`number'-0017-Data.txt") clear
		}
		if `quarter'==2 {
			infile using dictionary_fmly_1985, using("cex_files_old/82_83/DS0021/0`number'-0021-Data.txt") clear
		}
		if `quarter'==3 {
			infile using dictionary_fmly_1985, using("cex_files_old/82_83/DS0025/0`number'-0025-Data.txt") clear
		}
		if `quarter'==4 {
			infile using dictionary_fmly_1985, using("cex_files_old/82_83/DS0029/0`number'-0029-Data.txt") clear
		}
		if `quarter'==5 {
			infile using dictionary_fmly_1985, using("cex_files_old/82_83/DS0033/0`number'-0033-Data.txt") clear
		}	
	}	


	if `year'==1980 {
		if `quarter'==1 {
			infile using dictionary_fmly_1981, using("cex_files_old/80_81/DS0001/0`number'-0001-Data.txt") clear
		}
		if `quarter'==2 {
			infile using dictionary_fmly_1981, using("cex_files_old/80_81/DS0005/0`number'-0005-Data.txt") clear
		}
		if `quarter'==3 {
			infile using dictionary_fmly_1981, using("cex_files_old/80_81/DS0009/0`number'-0009-Data.txt") clear
		}
		if `quarter'==4 {
			infile using dictionary_fmly_1981, using("cex_files_old/80_81/DS0013/0`number'-0013-Data.txt") clear
		}
		if `quarter'==5 {
			infile using dictionary_fmly_1981, using("cex_files_old/80_81/DS0017/0`number'-0017-Data.txt") clear
		}	
	}	
	if `year'==1981 {
		if `quarter'==1 {
			infile using dictionary_fmly_1981, using("cex_files_old/80_81/DS0017/0`number'-0017-Data.txt") clear
		}
		if `quarter'==2 {
			infile using dictionary_fmly_1981, using("cex_files_old/80_81/DS0021/0`number'-0021-Data.txt") clear
		}
		if `quarter'==3 {
			infile using dictionary_fmly_1981, using("cex_files_old/80_81/DS0025/0`number'-0025-Data.txt") clear
		}
		if `quarter'==4 {
			infile using dictionary_fmly_1981, using("cex_files_old/80_81/DS0029/0`number'-0029-Data.txt") clear
		}
		if `quarter'==5 {
			infile using dictionary_fmly_1981, using("cex_files_old/80_81/DS0033/0`number'-0033-Data.txt") clear
		}	
	}	
	gen qintrvyr="19"+qintrvy2
	gen educ_ref_1d=educ_ref
	gen educ_2_1d=educa2
	capture gen desflg="00"

	
	if `year'<=1981 | `year'>=1986 {
			egen totexp2=rowtotal(totexp*)
			keep newid qintrvmo qintrvyr bls_urbn age_ref finlwt21 sex_ref marital1 fam_size no_earnr educ_ref_1d age2 sex2 educ_2_1d fsalaryx fsal_ryx fincbtax/*
			*/ fincbt_x fincatax fincat_x fjssdedx frrdedx fgovretx fpripenx findretx nonincmx pensionx respstat totexp2 perslt18 unemplx welfarex frretirx famtfedx/*
			*/ fedtaxx fedrfndx fsltaxx sloctaxx taxpropx misctaxx slrfundx othrfndx desflg inc_hrs2 inc_hrs1 incweek1 incweek2 region occupre1 occupre2
	}
	if `year'>=1984 & `year'<=1985 {
		egen totexp2=rowtotal(totexp*)
		egen cashco=rowtotal(cashco*)
		keep newid qintrvmo qintrvyr bls_urbn age_ref finlwt21 sex_ref marital1 fam_size no_earnr educ_ref_1d age2 sex2 educ_2_1d fsalaryx fsal_ryx fincbtax/*
		*/ fincbt_x fincatax fincat_x fjssdedx frrdedx fgovretx fpripenx findretx nonincmx pensionx respstat totexp2 perslt18 unemplx welfarex frretirx famtfedx/*
		*/ fedtaxx fedrfndx fsltaxx sloctaxx taxpropx misctaxx slrfundx othrfndx cashco desflg inc_hrs2 inc_hrs1 incweek1 incweek2 region occupre1 occupre2
	}
	
	
	
	
	}



gen cu_id=floor(newid/10)
gen intrvw=newid-cu_id*10


gen qtr=`quarter'+4*(`year'-1980)
gen qtr_5th=qtr+5-intrvw
gen long id=10000000*qtr_5th+cu_id
rename age_ref age

drop newid cu_id
order id intrvw qtr
save fmlyi, replace



/*read mtbi files*/
if `year'>=1996 {
	if `quarter'==1 {
		use "cex_files/intrvw`add'`YY'/mtbi`add'`YY'1x.dta", clear
	}
	if `quarter'==5 {
		use "cex_files/intrvw`add'`YY'/mtbi`add_next'`YY_next'1.dta", clear 
	}
	if `quarter'>1 & `quarter'<5 {
		use "cex_files/intrvw`add'`YY'/mtbi`add'`YY'`quarter'.dta", clear 
	}
}
if `year'<=1995 & `year'>=1985 {
		if `quarter'==1 {
			infile using dictionary_mtab_1980_1995, using("cex_files_old/`year'/DS0003/0`number'-0003-Data.txt") clear
		}
		if `quarter'==2 {
			infile using dictionary_mtab_1980_1995, using("cex_files_old/`year'/DS0007/0`number'-0007-Data.txt") clear
		}
		if `quarter'==3 {
			infile using dictionary_mtab_1980_1995, using("cex_files_old/`year'/DS0011/0`number'-0011-Data.txt") clear
		}
		if `quarter'==4 {
			infile using dictionary_mtab_1980_1995, using("cex_files_old/`year'/DS0015/0`number'-0015-Data.txt") clear
		}
		if `quarter'==5 {
			infile using dictionary_mtab_1980_1995, using("cex_files_old/`year'/DS0019/0`number'-0019-Data.txt") clear
		}
}	
if `year'==1984 {
		if `quarter'==1 {
			use "cex_files_old/`year'/DS0003/0`number'-0003-Data.dta", clear
		}
		if `quarter'==2 {
			use "cex_files_old/`year'/DS0007/0`number'-0007-Data.dta", clear
		}
		if `quarter'==3 {
			use "cex_files_old/`year'/DS0011/0`number'-0011-Data.dta", clear
		}
		if `quarter'==4 {
			use "cex_files_old/`year'/DS0015/0`number'-0015-Data.dta", clear
		}
		if `quarter'==5 {
			use "cex_files_old/`year'/DS0019/0`number'-0019-Data.dta", clear
		}	
		
		rename *, lower /*make all lowercase*/
}
if `year'==1982 {
		if `quarter'==1 {
			infile using dictionary_mtab_1980_1995, using("cex_files_old/82_83/DS0003/0`number'-0003-Data.txt") clear
		}
		if `quarter'==2 {
			infile using dictionary_mtab_1980_1995, using("cex_files_old/82_83/DS0007/0`number'-0007-Data.txt") clear
		}
		if `quarter'==3 {
			infile using dictionary_mtab_1980_1995, using("cex_files_old/82_83/DS0011/0`number'-0011-Data.txt") clear
		}
		if `quarter'==4 {
			infile using dictionary_mtab_1980_1995, using("cex_files_old/82_83/DS0015/0`number'-0015-Data.txt") clear
		}
		if `quarter'==5 {
			infile using dictionary_mtab_1980_1995, using("cex_files_old/82_83/DS0019/0`number'-0019-Data.txt") clear
		}
}	
if `year'==1983 {
		if `quarter'==1 {
			infile using dictionary_mtab_1980_1995, using("cex_files_old/82_83/DS0019/0`number'-0019-Data.txt") clear
		}
		if `quarter'==2 {
			infile using dictionary_mtab_1980_1995, using("cex_files_old/82_83/DS0023/0`number'-0023-Data.txt") clear
		}
		if `quarter'==3 {
			infile using dictionary_mtab_1980_1995, using("cex_files_old/82_83/DS0027/0`number'-0027-Data.txt") clear
		}
		if `quarter'==4 {
			infile using dictionary_mtab_1980_1995, using("cex_files_old/82_83/DS0031/0`number'-0031-Data.txt") clear
		}
		if `quarter'==5 {
			infile using dictionary_mtab_1980_1995, using("cex_files_old/82_83/DS0035/0`number'-0035-Data.txt") clear
		}
}	
if `year'==1980 {
		if `quarter'==1 {
			infile using dictionary_mtab_1980_1995, using("cex_files_old/80_81/DS0003/0`number'-0003-Data.txt") clear
		}
		if `quarter'==2 {
			infile using dictionary_mtab_1980_1995, using("cex_files_old/80_81/DS0007/0`number'-0007-Data.txt") clear
		}
		if `quarter'==3 {
			infile using dictionary_mtab_1980_1995, using("cex_files_old/80_81/DS0011/0`number'-0011-Data.txt") clear
		}
		if `quarter'==4 {
			infile using dictionary_mtab_1980_1995, using("cex_files_old/80_81/DS0015/0`number'-0015-Data.txt") clear
		}
		if `quarter'==5 {
			infile using dictionary_mtab_1980_1995, using("cex_files_old/80_81/DS0019/0`number'-0019-Data.txt") clear
		}
}	
if `year'==1981 {
		if `quarter'==1 {
			infile using dictionary_mtab_1980_1995, using("cex_files_old/80_81/DS0019/0`number'-0019-Data.txt") clear
		}
		if `quarter'==2 {
			infile using dictionary_mtab_1980_1995, using("cex_files_old/80_81/DS0023/0`number'-0023-Data.txt") clear
		}
		if `quarter'==3 {
			infile using dictionary_mtab_1980_1995, using("cex_files_old/80_81/DS0027/0`number'-0027-Data.txt") clear
		}
		if `quarter'==4 {
			infile using dictionary_mtab_1980_1995, using("cex_files_old/80_81/DS0031/0`number'-0031-Data.txt") clear
		}
		if `quarter'==5 {
			infile using dictionary_mtab_1980_1995, using("cex_files_old/80_81/DS0035/0`number'-0035-Data.txt") clear
		}
}	
keep newid ucc cost
destring newid, replace
drop if newid==.
save mexpendi, replace


/*read itbi files*/
if `year'>=1996 {
	if `quarter'==1 {
		use "cex_files/intrvw`add'`YY'/itbi`add'`YY'1x.dta", clear
	}
	if `quarter'==5 {
		use "cex_files/intrvw`add'`YY'/itbi`add_next'`YY_next'1.dta", clear 
	}
	if `quarter'>1 & `quarter'<5 {
		use "cex_files/intrvw`add'`YY'/itbi`add'`YY'`quarter'.dta", clear 
	}
}
if `year'<=1995 & `year'>=1985{
		if `quarter'==1 {
			infile using dictionary_itab_1980_1995, using("cex_files_old/`year'/DS0004/0`number'-0004-Data.txt") clear
		}
		if `quarter'==2 {
			infile using dictionary_itab_1980_1995, using("cex_files_old/`year'/DS0008/0`number'-0008-Data.txt") clear
		}
		if `quarter'==3 {
			infile using dictionary_itab_1980_1995, using("cex_files_old/`year'/DS0012/0`number'-0012-Data.txt") clear
		}
		if `quarter'==4 {
			infile using dictionary_itab_1980_1995, using("cex_files_old/`year'/DS0016/0`number'-0016-Data.txt") clear
		}
		if `quarter'==5 {
			infile using dictionary_itab_1980_1995, using("cex_files_old/`year'/DS0020/0`number'-0020-Data.txt") clear
		}
}

if `year'==1984 {
		if `quarter'==1 {
			use "cex_files_old/`year'/DS0004/0`number'-0004-Data.dta", clear
		}
		if `quarter'==2 {
			use "cex_files_old/`year'/DS0008/0`number'-0008-Data.dta", clear
		}
		if `quarter'==3 {
			use "cex_files_old/`year'/DS0012/0`number'-0012-Data.dta", clear
		}
		if `quarter'==4 {
			use "cex_files_old/`year'/DS0016/0`number'-0016-Data.dta", clear
		}
		if `quarter'==5 {
			use "cex_files_old/`year'/DS0020/0`number'-0020-Data.dta", clear
		}	
		
		rename *, lower /*make all lowercase*/
		rename cost value
}
if `year'==1982 {
		if `quarter'==1 {
			infile using dictionary_itab_1980_1995, using("cex_files_old/82_83/DS0004/0`number'-0004-Data.txt") clear
		}
		if `quarter'==2 {
			infile using dictionary_itab_1980_1995, using("cex_files_old/82_83/DS0008/0`number'-0008-Data.txt") clear
		}
		if `quarter'==3 {
			infile using dictionary_itab_1980_1995, using("cex_files_old/82_83/DS0012/0`number'-0012-Data.txt") clear
		}
		if `quarter'==4 {
			infile using dictionary_itab_1980_1995, using("cex_files_old/82_83/DS0016/0`number'-0016-Data.txt") clear
		}
		if `quarter'==5 {
			infile using dictionary_itab_1980_1995, using("cex_files_old/82_83/DS0020/0`number'-0020-Data.txt") clear
		}
}	
if `year'==1983 {
		if `quarter'==1 {
			infile using dictionary_itab_1980_1995, using("cex_files_old/82_83/DS0020/0`number'-0020-Data.txt") clear
		}
		if `quarter'==2 {
			infile using dictionary_itab_1980_1995, using("cex_files_old/82_83/DS0024/0`number'-0024-Data.txt") clear
		}
		if `quarter'==3 {
			infile using dictionary_itab_1980_1995, using("cex_files_old/82_83/DS0028/0`number'-0028-Data.txt") clear
		}
		if `quarter'==4 {
			infile using dictionary_itab_1980_1995, using("cex_files_old/82_83/DS0032/0`number'-0032-Data.txt") clear
		}
		if `quarter'==5 {
			infile using dictionary_itab_1980_1995, using("cex_files_old/82_83/DS0036/0`number'-0036-Data.txt") clear
		}
}	

if `year'==1980 {
		if `quarter'==1 {
			infile using dictionary_itab_1980_1995, using("cex_files_old/80_81/DS0004/0`number'-0004-Data.txt") clear
		}
		if `quarter'==2 {
			infile using dictionary_itab_1980_1995, using("cex_files_old/80_81/DS0008/0`number'-0008-Data.txt") clear
		}
		if `quarter'==3 {
			infile using dictionary_itab_1980_1995, using("cex_files_old/80_81/DS0012/0`number'-0012-Data.txt") clear
		}
		if `quarter'==4 {
			infile using dictionary_itab_1980_1995, using("cex_files_old/80_81/DS0016/0`number'-0016-Data.txt") clear
		}
		if `quarter'==5 {
			infile using dictionary_itab_1980_1995, using("cex_files_old/80_81/DS0020/0`number'-0020-Data.txt") clear
		}
}	
if `year'==1981 {
		if `quarter'==1 {
			infile using dictionary_itab_1980_1995, using("cex_files_old/80_81/DS0020/0`number'-0020-Data.txt") clear
		}
		if `quarter'==2 {
			infile using dictionary_itab_1980_1995, using("cex_files_old/80_81/DS0024/0`number'-0024-Data.txt") clear
		}
		if `quarter'==3 {
			infile using dictionary_itab_1980_1995, using("cex_files_old/80_81/DS0028/0`number'-0028-Data.txt") clear
		}
		if `quarter'==4 {
			infile using dictionary_itab_1980_1995, using("cex_files_old/80_81/DS0032/0`number'-0032-Data.txt") clear
		}
		if `quarter'==5 {
			infile using dictionary_itab_1980_1995, using("cex_files_old/80_81/DS0036/0`number'-0036-Data.txt") clear
		}
}	

keep newid ucc value
rename value cost
destring newid, replace
drop if newid==.
save iexpendi, replace


use mexpendi, clear
append using iexpendi


replace cost=-cost if ucc=="820301" | ucc=="820302" | ucc=="790830"
replace cost=cost*4 if ucc=="710110"
replace cost=cost*12 if ucc=="910050"

drop if (ucc=="340915") & ((`year'>=2002 & `year'<=2004) | (`year'==2005 & `quarter'==1))
drop if (ucc=="800111" | ucc=="800121" | ucc=="800804") & (`year'<=2000 | (`year'==2001 & `quarter'==1))
drop if (ucc=="690110") & (`year'==1988) & (`quarter'==1)

gen cu_id=floor(newid/10)
gen intrvw=newid-cu_id*10


gen qtr=`quarter'+4*(`year'-1980)
gen qtr_5th=qtr+5-intrvw
gen long id=10000000*qtr_5th+cu_id
save expendi, replace



use expendi, clear
sort id

if `year'<=1993 | (`year'==1994 & `quarter'!=5) {
	gen aux_var=0
	replace aux_var=1 if ucc=="190904" | (ucc>="790220" & ucc<="790230")  
	by id: egen fdhome=total(cost*aux_var)
	drop aux_var 

	gen aux_var=0
	replace aux_var=1 if (ucc>="190901" & ucc<="190903") | ucc=="790410" | ucc=="790430" | ucc=="800700" 
	by id: egen fdaway=total(cost*aux_var)
	drop aux_var 

	gen aux_var=0
	replace aux_var=1 if ucc=="200900" | (ucc>="790310" & ucc<="790320") | ucc=="790420"
	by id: egen alcbev=total(cost*aux_var)
	drop aux_var 

	gen aux_var=0
	replace aux_var=1 if ucc=="210210" | ucc=="210310" | ucc=="210902" | ucc=="220112" | ucc=="220122" | ucc=="220212" | ucc=="220312" | ucc=="220314" | ucc=="220322" | ucc=="220902" | ucc=="230123" | ucc=="230152"/*
					*/ | ucc=="230902" | ucc=="240113" | ucc=="240123" | ucc=="240214" | ucc=="240223" | ucc=="240313" | ucc=="240323" | ucc=="320613" | ucc=="320623" | ucc=="320633" | ucc=="340912" | ucc=="880310" | ucc=="990940" | ucc=="230119"
	by id: egen othold=total(cost*aux_var)
	drop aux_var 
	
	gen aux_var=0
	replace aux_var=1 if ucc=="210110" | ucc=="230121" | ucc=="230141" | ucc=="230150" | ucc=="240111" | ucc=="240121" | ucc=="240211" | ucc=="240221" | ucc=="240311" | ucc=="240321" | ucc=="320611" | ucc=="320621"/*
					*/ | ucc=="320631" | ucc=="350110" | ucc=="790690" | ucc=="800710" | ucc=="230111" | (ucc>="990910" & ucc<="990920")
	by id: egen rendwe=total(cost*aux_var)
	drop aux_var 	

	gen aux_var=0
	replace aux_var=1 if ucc=="270101" | ucc=="270102" | ucc=="270000"
	by id: egen teleph=total(cost*aux_var)
	drop aux_var 

	gen aux_var=0
	replace aux_var=1 if (ucc>="250111" & ucc<="270214") | (ucc>="270411" & ucc<="270904")
	by id: egen util=total(cost*aux_var)
	drop aux_var 

	gen aux_var=0
	replace aux_var=1 if ucc=="340310" | ucc=="340410" | ucc=="340420" | ucc=="340520" | ucc=="340530" | ucc=="340903" | ucc=="340906" | ucc=="340914"
	by id: egen dmsxcc=total(cost*aux_var)
	drop aux_var 	

	gen aux_var=0
	replace aux_var=1 if ucc=="340211" | ucc=="340212" | ucc=="670310" | ucc=="340210"
	by id: egen bbyday=total(cost*aux_var)
	drop aux_var 	

	gen aux_var=0
	replace aux_var=1 if (ucc>="280110" & ucc<="280900")
	by id: egen textil=total(cost*aux_var)
	drop aux_var 		

	gen aux_var=0
	replace aux_var=1 if (ucc>="290110" & ucc<="290440")
	by id: egen furntr=total(cost*aux_var)
	drop aux_var 	

	gen aux_var=0
	replace aux_var=1 if (ucc>="230131" & ucc<="230132") | ucc=="320110" | (ucc>="320161" & ucc<="320162")
	by id: egen flrcvr=total(cost*aux_var)
	drop aux_var	

	gen aux_var=0
	replace aux_var=1 if (ucc>="230117" & ucc<="230118") | (ucc>="300111" & ucc<="300412") | (ucc>="320511" & ucc<="320512")
	by id: egen majapp=total(cost*aux_var)
	drop aux_var		

	gen aux_var=0
	replace aux_var=1 if (ucc>="320310" & ucc<="320370") | (ucc>="320521" & ucc<="320522")
	by id: egen smlapp=total(cost*aux_var)
	drop aux_var	

	gen aux_var=0
	replace aux_var=1 if (ucc>="320120" & ucc<="320150") | (ucc>="320210" & ucc<="320232") | (ucc>="320410" & ucc<="320420") | (ucc>="320901" & ucc<="320904") | ucc=="340904" | ucc=="430130" | (ucc>="690111" & ucc<="690112")/*
							*/ | (ucc>="690210" & ucc<="690245") | ucc=="690110" | ucc=="690117"
	by id: egen misceq=total(cost*aux_var)
	drop aux_var	

	gen aux_var=0
	replace aux_var=1 if (ucc>="360110" & ucc<="360902")
	by id: egen mensix=total(cost*aux_var)
	drop aux_var		
	
	gen aux_var=0
	replace aux_var=1 if (ucc>="380110" & ucc<="380903")
	by id: egen womsix=total(cost*aux_var)
	drop aux_var		
	
	gen aux_var=0
	replace aux_var=1 if (ucc>="370110" & ucc<="370902")
	by id: egen boyfif=total(cost*aux_var)
	drop aux_var		
	
	gen aux_var=0
	replace aux_var=1 if (ucc>="390110" & ucc<="390902")
	by id: egen grlfif=total(cost*aux_var)
	drop aux_var		
	
	gen aux_var=0
	replace aux_var=1 if (ucc>="410110" & ucc<="410904")
	by id: egen chldrn=total(cost*aux_var)
	drop aux_var		
	
	gen aux_var=0
	replace aux_var=1 if (ucc>="400110" & ucc<="400310")
	by id: egen footwr=total(cost*aux_var)
	drop aux_var		
	
	gen aux_var=0
	replace aux_var=1 if (ucc>="420110" & ucc<="430120") | (ucc>="440110" & ucc<="440900")
	by id: egen othapl=total(cost*aux_var)
	drop aux_var		
	
	gen aux_var=0
	replace aux_var=1 if ucc=="450110" | ucc=="450210"
	by id: egen cartkn=total(cost*aux_var)
	drop aux_var	
	
	gen aux_var=0
	replace aux_var=1 if ucc=="460110" | ucc=="460901"
	by id: egen cartku=total(cost*aux_var)
	drop aux_var		
	
	gen aux_var=0
	replace aux_var=1 if ucc=="450220" | ucc=="460902"
	by id: egen othveh=total(cost*aux_var)
	drop aux_var		
	
	gen aux_var=0
	replace aux_var=1 if ucc=="850300" | (ucc>="510110" & ucc<="510902")
	by id: egen vehfin=total(cost*aux_var)
	drop aux_var	
	
	gen aux_var=0
	replace aux_var=1 if ucc=="450310" | (ucc>="450313" & ucc<="450314") | ucc=="450410" | (ucc>="450413" & ucc<="450414") | (ucc>="520110" & ucc<="520550") | (ucc>="520902" & ucc<="520903") | (ucc>="520905" & ucc<="520906") | ucc=="620113"
	by id: egen vrntlo=total(cost*aux_var)
	drop aux_var		
	
	gen aux_var=0
	replace aux_var=1 if ucc=="500110"
	by id: egen vehins=total(cost*aux_var)
	drop aux_var	
	
	gen aux_var=0
	replace aux_var=1 if (ucc>="470111" & ucc<="470212")
	by id: egen gasmo=total(cost*aux_var)
	drop aux_var		
	
	gen aux_var=0
	replace aux_var=1 if (ucc>="470220" & ucc<="490900")
	by id: egen mainrp=total(cost*aux_var)
	drop aux_var		
	
	gen aux_var=0
	replace aux_var=1 if (ucc>="530110" & ucc<="530902")
	by id: egen pubtra=total(cost*aux_var)
	drop aux_var	
	
	gen aux_var=0
	replace aux_var=1 if (ucc>="540000" & ucc<="580902")
	by id: egen health=total(cost*aux_var)
	drop aux_var	
	
	gen aux_var=0
	replace aux_var=1 if (ucc>="610900" & ucc<="620111") | (ucc>="620121" & ucc<="620310") | ucc=="620903"
	by id: egen feeadm=total(cost*aux_var)
	drop aux_var		
	
	gen aux_var=0
	replace aux_var=1 if ucc=="660310" | ucc=="590110" | (ucc>="590111" & ucc<="590230")
	by id: egen read=total(cost*aux_var)
	drop aux_var	
	
	gen aux_var=0
	replace aux_var=1 if ucc=="270310" | (ucc>="310110" & ucc<="310342") | ucc=="340610" | ucc=="340902" | ucc=="340905" | ucc=="610130" | ucc=="620904" | ucc=="620912"
	by id: egen tvrdio=total(cost*aux_var)
	drop aux_var	
	
	gen aux_var=0
	replace aux_var=1 if ucc=="520901" | ucc=="520904" | ucc=="520907" | ucc=="600132" | ucc=="600141" | ucc=="600142" | ucc=="600131" | (ucc>="600110" & ucc<="600122") | (ucc>="600210" & ucc<="610120") | (ucc>="610210" & ucc<="610320")/*
						*/ | (ucc>="620330" & ucc<="620420") | (ucc>="620905" & ucc<="620909") | (ucc>="620919" & ucc<="620922") | ucc=="620902"
	by id: egen otheqp=total(cost*aux_var)
	drop aux_var	
	
	gen aux_var=0
	replace aux_var=1 if (ucc>="640130" & ucc<="650900")
	by id: egen persca=total(cost*aux_var)
	drop aux_var		
	
	gen aux_var=0
	replace aux_var=1 if (ucc>="660110" & ucc<="660210") | (ucc>="660900" & ucc<="670210") | (ucc>="670901" & ucc<="670903")
	by id: egen educa=total(cost*aux_var)
	drop aux_var	
	
	gen aux_var=0
	replace aux_var=1 if (ucc>="630110" & ucc<="630210")
	by id: egen tobacc=total(cost*aux_var)
	drop aux_var	
	
	gen aux_var=0
	replace aux_var=1 if ucc=="210901" | ucc=="220111" | ucc=="220121" | ucc=="220211" | ucc=="220311" | ucc=="220313" | ucc=="220321" | ucc=="220901" | (ucc>="230112" & ucc<="230115") | ucc=="230122" | ucc=="230142"/*
					*/ | ucc=="230151" | ucc=="230901" | ucc=="240112" | ucc=="240122" | (ucc>="240212" & ucc<="240213") | ucc=="240222" | ucc=="240312" | ucc=="240322" | ucc=="320612" | ucc=="320622" | ucc=="320632" | ucc=="340911" | ucc=="880110"/*
					*/ | ucc=="990930" | ucc=="230116"
	by id: egen owndwe=total(cost*aux_var)
	drop aux_var		
	
	gen aux_var=0
	replace aux_var=1 if ucc=="620112" | (ucc>="680110" & ucc<="680902") | ucc=="710110" | ucc=="790600" | ucc=="880210" | ucc=="900001"
	by id: egen miscx4=total(cost*aux_var)
	drop aux_var
	
	gen aux_var=0
	replace aux_var=1 if ucc=="002120" | ucc=="700110"
	by id: egen lifins=total(cost*aux_var)
	drop aux_var		
	
	gen aux_var=0
	replace aux_var=1 if (ucc>="800910" & ucc<="800940")
	by id: egen retpen=total(cost*aux_var)
	drop aux_var	
	
	gen aux_var=0
	replace aux_var=1 if ucc=="800700"
	by id: egen fdmap=total(cost*aux_var)
	drop aux_var	
	
	gen aux_var=0
	replace aux_var=1 if ucc=="220211"
	by id: egen proptx=total(cost*aux_var)
	drop aux_var
	
	
	if (`year'>=1986 & `year'<=1994) | (`year'<=1983) {
		gen aux_var=0
		replace aux_var=1 if ucc=="800801" | (ucc>="800810" & ucc<="800860")
		by id: egen cashco=total(cost*aux_var)
		drop aux_var
	}
	
	if (`year'==1982) | (`year'==1983) {
		*drop totexp2
		gen aux_var=0
		replace aux_var=1 if ucc=="002120" | (ucc>="190901" & ucc<="220322") | (ucc>="220901" & ucc<="450110") | ucc=="450210" | ucc=="450220" | ucc=="450310" | (ucc>="450313" & ucc<="450410") | (ucc>="450413" & ucc<="450414")/*
					*/ | ucc=="460110" | (ucc>="460901" & ucc<="460902") | (ucc>="470111" & ucc<="600122") | (ucc>="600131" & ucc<="600132") | ucc=="600141" | ucc=="600142" | (ucc>="600210" & ucc<="710110") | (ucc>="790220" & ucc<="790600")/*
					*/ | ucc=="790690" | (ucc>="800700" & ucc<="800710") | ucc=="800801" | (ucc>="800810" & ucc<="800940") | ucc=="850300" | ucc=="880110" | ucc=="880210" | ucc=="880310" | ucc=="900001"| (ucc>="990900" & ucc<="990940")
		by id: egen totexp2=total(cost*aux_var)
		drop aux_var		
	}
	
	gen shelt=rendwe+othold
	replace util=util-teleph
	gen furnsh=textil+furntr+flrcvr
	gen equpmt=majapp+smlapp+misceq+teleph
	replace chldrn=boyfif+grlfif+chldrn
	gen vehpch=cartkn+cartku+othveh+vehfin+vrntlo+vehins
	gen vehopr=gasmo+mainrp
	gen feeard=feeadm+read
	gen tvrdot=tvrdio+otheqp
	
	drop rendwe othold textil furntr flrcvr majapp smlapp misceq teleph boyfif grlfif cartkn cartku othveh vehfin vehins gasmo mainrp feeadm read tvrdio otheqp
}

/*
gen aux_var=0
replace aux_var=1 if ucc=="001000" | ucc=="001010" | ucc=="001210" | ucc=="001220" | ucc=="002010" | ucc=="002020" | ucc=="002030" | ucc=="003000" | ucc=="003100" | ucc=="790630" | ucc=="790710" | ucc=="790730"/*
*/ | ucc=="790810" | ucc=="790820" | ucc=="790830" | ucc=="810101" | ucc=="810102" | ucc=="810301" | ucc=="810302" | ucc=="820101" | ucc=="820102" | ucc=="820201" | ucc=="820202" | ucc=="820301" | ucc=="820302" | ucc=="840101"/*
*/ | ucc=="840102" | ucc=="220611" | ucc=="220616" | ucc=="220512" | ucc=="220612" | ucc=="990950" | ucc=="220615" | ucc=="220513" | ucc=="790610" | ucc=="790620" | ucc=="790611" | ucc=="220511" | ucc=="220614" | ucc=="860300"
by id: egen chgass=total(cost*aux_var)
drop aux_var 


gen aux_var=0
replace aux_var=1 if ucc=="790910" | ucc=="790920" | ucc=="790930" | ucc=="790940" | ucc=="790950" | ucc=="830101" | ucc=="830102" | ucc=="830201" | ucc=="830202" | ucc=="830203" | ucc=="830204" | ucc=="830301"/*
*/ | ucc=="830302" | ucc=="830303" | ucc=="830304" | ucc=="850100" | ucc=="850200" | ucc=="880120" | ucc=="880220" | ucc=="880320" 
sort id aux_var
by id: egen chglia=total(cost*aux_var)
drop aux_var 
*/

gen aux_var=0
replace aux_var=1 if ucc=="006001"
sort id aux_var
by id: egen u06001=total(cost*aux_var)
drop aux_var 

gen aux_var=0
replace aux_var=1 if ucc=="006002"
sort id aux_var
by id: egen u06002=total(cost*aux_var)
drop aux_var 

gen aux_var=0
replace aux_var=1 if ucc=="330511" | ucc=="340510" /*pest control && moving, storage, freight*/
sort id aux_var
by id: egen owndw2=total(cost*aux_var)
drop aux_var 

gen aux_var=0
replace aux_var=1 if ucc=="340915" /*home security service fee*/
sort id aux_var
by id: egen dmsxc2=total(cost*aux_var)
drop aux_var 

gen aux_var=.
replace aux_var=1 if ucc=="340630" /*Reupholstering, furniture repair*/
sort id aux_var
by id: egen furns2=total(cost*aux_var)
drop aux_var 

gen aux_var=0
replace aux_var=1 if ucc=="340620" | ucc=="340901" | ucc=="340907" | ucc=="340908" | ucc=="690113" | ucc=="690114" | ucc=="690116" | ucc=="990900" | ucc=="690310"
/*Appliance repair, including service center  &&  Repairs/rentals of lawn and garden equipment && Appliance rental && Rental of office equipment for nonbusiness use*/
/*Repair of computer systems for nonbusiness use   &&  Computer information services && Internet services away from home && Rental and installation of dishwashers && Installation of computer*/
sort id aux_var
by id: egen equpm2=total(cost*aux_var)
drop aux_var 

gen aux_var=0
replace aux_var=1 if ucc=="860100" | ucc=="860200" | ucc=="860300" | ucc=="860301" | ucc=="860302" | ucc=="860400" | ucc=="860500" | ucc=="860600" | ucc=="860700"
/*Sale of automobiles && Sale of trucks, including vans  && Sale of motor camper && Sale of other vehicles && Sale of trailer type and other attachable campers && Sale of motorcycles*/
/*Sale of boats, with motors && Sale of boats, without motors and boat trailers*/
sort id aux_var
by id: egen vehpc2=total(cost*aux_var)
drop aux_var 

gen aux_var=0
replace aux_var=1 if ucc=="800111" | ucc=="800121" | ucc=="800804" | ucc=="800801"
/*Alimony expenditures &&  Child support expenditure && Support for college students*/
sort id aux_var
by id: egen alchcl=total(cost*aux_var)
drop aux_var 

gen aux_var=0
replace aux_var=1 if ucc=="910050"
sort id aux_var
by id: egen reqown=total(cost*aux_var)
/*Estimated monthly rental value of owned home*/
drop aux_var 


***********************************************************
*************RECREATION CATEGORIES*************************
***********************************************************
	
	
**VIDEO-AUDIO extended
gen aux_var=0
replace aux_var=1 if ucc=="270310" | ucc=="270311" |/*
*/ (ucc>="310110" & ucc<="310350") | ucc=="310400" |/*
*/ ucc=="340610" | ucc=="340902" | ucc=="340905" | ucc=="610130" | ucc=="620904" | ucc=="620912" | ucc=="620930"  | ucc=="620916" | ucc=="620917" | ucc=="620918"
sort id aux_var
by id: egen rec_audio_video=total(cost*aux_var)
drop aux_var 


**VIDEO-AUDIO narrow
gen aux_var=0
replace aux_var=1 if ucc=="270310" | ucc=="270311" |/*
*/ (ucc>="310110" & ucc<="310350") | ucc=="310400"
sort id aux_var
by id: egen rec_audio_video_narrow=total(cost*aux_var)
drop aux_var 

**CABLE
gen aux_var=0
replace aux_var=1 if ucc=="270310" | ucc=="270311"
sort id aux_var
by id: egen rec_cabel=total(cost*aux_var)
drop aux_var 

	
**OTHER SERVICES
gen aux_var=0
replace aux_var=1 if (ucc>="610900" & ucc<="620111") | (ucc>="620121" & ucc<="620310") | ucc=="620903"
sort id aux_var
by id: egen rec_oth_serv=total(cost*aux_var)
gen rec_oth_serv_narrow=rec_oth_serv
drop aux_var 



**SPORTS_extended
gen aux_var=0
replace aux_var=1 if ucc=="520901" | ucc=="520904" | ucc=="520907" | ucc=="600132" | ucc=="600141" | ucc=="600142" | /*
*/ ucc=="600131" | (ucc>="600110" & ucc<="600122") | (ucc>="600210" & ucc<"610000") | /*
*/ (ucc>="620906" & ucc<="620909") | (ucc>="620919" & ucc<="620922") | ucc=="620902" | ucc=="600127" | ucc=="600128" | ucc=="600137" | ucc=="600138"
sort id aux_var
by id: egen rec_sport=total(cost*aux_var)
drop aux_var 

**SPORTS_narrow
gen aux_var=0
replace aux_var=1 if ucc=="600132" | ucc=="600141" | ucc=="600142" | ucc=="600131" | (ucc>="600110" & ucc<="600122") | (ucc>="600210" & ucc<"610000") | /*
*/ ucc=="600127" | ucc=="600128" | ucc=="600137" | ucc=="600138"
sort id aux_var
by id: egen rec_sport_narrow=total(cost*aux_var)
drop aux_var 


**PHOTO_extended
gen aux_var=0
replace aux_var=1 if ucc=="610210" | ucc=="620330" | ucc=="620905" | ucc=="610230" | ucc=="620320"
sort id aux_var
by id: egen rec_photo=total(cost*aux_var)
drop aux_var 

**PHOTO_narrow (no photographers and photo processing)
gen aux_var=0
replace aux_var=1 if ucc=="610210" | ucc=="610230" 
sort id aux_var
by id: egen rec_photo_narrow=total(cost*aux_var)
drop aux_var 

**PETS_extended
gen aux_var=0
replace aux_var=1 if ucc=="610320" | ucc=="620410" | ucc=="620420"
sort id aux_var
by id: egen rec_pets=total(cost*aux_var)
drop aux_var 

**PETS_narrow (no vet and pet services)
gen aux_var=0
replace aux_var=1 if ucc=="610320"
sort id aux_var
by id: egen rec_pets_narrow=total(cost*aux_var)
drop aux_var 


**READING
gen aux_var=0
replace aux_var=1 if ucc=="660310" | (ucc>="590110" & ucc<="590230") | ucc=="590310" | ucc=="590410" | ucc=="690118"
sort id aux_var
by id: egen rec_reading=total(cost*aux_var)
gen rec_reading_narrow=rec_reading
drop aux_var 
	

**OHTER GOODS
gen aux_var=0
replace aux_var=1 if ucc=="610110" | ucc=="610140" | ucc=="610120" | ucc=="610130" | ucc=="620904"
sort id aux_var
by id: egen rec_oth_goods=total(cost*aux_var)
drop aux_var 

**OHTER GOODS narrow
gen aux_var=0
replace aux_var=1 if ucc=="610110" | ucc=="610140" | ucc=="610120" | ucc=="610130"
sort id aux_var
by id: egen rec_oth_goods_narrow=total(cost*aux_var)
drop aux_var 



quietly by id:  gen dup = cond(_N==1,0,_n)
drop if dup>1
drop dup newid ucc cost cu_id intrvw qtr qtr_5th

save expendi2, replace

use fmlyi, clear
sort id
merge 1:1 id using expendi2
keep if _merge==3
drop _merge
replace intrvw=intrvw+1 if desflg=="10" /*correct for sample consutruction after 2015 */
replace intrvw=intrvw+1 if `year'>=2016 | (`year'==2015 & `quarter'==5)
drop desflg
/* intrvw shows the number of the interview for this particular household. If quarter=1 then delete households for whom it is last interview. If quarter=5 (next year quarter 1), delete if this interview is NOT las */
drop if (intrvw==5 & `quarter'==1) 
drop if (intrvw!=5 & `quarter'==5) 

replace fdhome=fdhome/(exp(-0.10795)) if (`year'>=1982 & `year'<=1987)
replace cashco=4*cashco if (`year'>=1984 & `year'<=1985) | (`year'>=1995 & `year'<=2000) | (`year'==2001 & `quarter'==2 & qtr==qtr_5th)
replace cashco=2*cashco if `year'==2001 & ((`quarter'==2 & qtr==qtr_5th-1) | (`quarter'==3 & qtr==qtr_5th))
replace cashco=4/3*cashco if `year'==2001 & ((`quarter'==2 & qtr==qtr_5th-2) | (`quarter'==3 & qtr==qtr_5th-1) | (`quarter'==4 & qtr==qtr_5th))

save processed_`year'_`quarter', replace

sleep 1000
erase fmlyi.dta
erase expendi2.dta
erase expendi.dta
erase iexpendi.dta
erase mexpendi.dta




}
}







forvalues year=`start_year'(1)`finish_year' {
	forvalues quarter=1(1)5{
		use processed_`year'_`quarter', clear
		destring marital1, replace
		destring sex_ref, replace
		destring sex2, replace
		destring inc_hrs1, replace
		destring incweek1, replace
		destring inc_hrs2, replace
		destring incweek2, replace
		save processed_`year'_`quarter', replace
	}
}





forvalues year=`start_year'(1)`finish_year' {
	forvalues quarter=1(1)5{
		append using processed_`year'_`quarter'
	}
}



save cex_for_processing, replace

forvalues year=`start_year'(1)`finish_year' {
	forvalues quarter=1(1)5{
		erase processed_`year'_`quarter'.dta

	}
}


